package com.newsee.remote.shassets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newsee.remote.common.database.entity.ShAssets;
import com.newsee.remote.common.database.mapper.ShAssetsMapper;
import com.newsee.remote.common.dto.assets.AssIRequestDTO;
import com.newsee.remote.common.dto.assets.AssReqBaseInfoDTO;
import com.newsee.remote.common.dto.assets.AssReqItemDTO;
import com.newsee.remote.common.dto.assets.AssetsDTO;
import com.newsee.remote.common.enumeration.SrmRspStatusCodeEnum;
import com.newsee.remote.common.utils.tools.DateUtils;
import com.newsee.remote.common.vo.assets.AssetsVO;
import com.newsee.remote.common.vo.supplier.EResponseVO;
import com.newsee.remote.common.vo.supplier.MessageVO;
import com.newsee.remote.common.vo.supplier.RspBaseInfoVO;
import com.newsee.remote.common.vo.supplier.RspItemVO;
import com.newsee.remote.shassets.service.IShAssetsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liangzhichen
 * @since 2019-11-11
 */
@Slf4j
@Service
public class ShAssetsServiceImpl extends ServiceImpl<ShAssetsMapper, ShAssets> implements IShAssetsService {

    @Override
    public AssetsVO acceptSapAssets(AssetsDTO dto) {
        AssetsVO vo = new AssetsVO();

        if (!checkData(dto)) {
            generatorRspBaseInfo(dto, vo, SrmRspStatusCodeEnum.SEND_MESSAGE_ERROR.getCode());
            return vo;
        }

        try {
            //srm提供的供应商报文
            List<AssReqItemDTO> heads = dto.getI_REQUEST().getMESSAGE().getREQ_ITEM();

            for (AssReqItemDTO head : heads) {
                //唯一标识
                String budgetItemCode = head.getINVNR();
                ShAssets shAssets = getOne(new QueryWrapper<ShAssets>().eq("AssetsCode", budgetItemCode));

                //新增
                if (null == shAssets) {
                    shAssets = new ShAssets();
                    shAssets.setCreateDate(LocalDateTime.now());
                } else {
                    shAssets.setUpdateDate(LocalDateTime.now());
                }
                shAssets.setAssetsCode(budgetItemCode);

                //入账日期
                if (null != head.getAKTIV()) {
                    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
                    //初始化日期,写死成
                    if("00000000".equals(head.getAKTIV())){
                        shAssets.setAccountDate(LocalDateTime.from(LocalDate.parse("20200103", df).atStartOfDay()));
                    }else {
                        shAssets.setAccountDate(LocalDateTime.from(LocalDate.parse(head.getAKTIV(), df).atStartOfDay()));
                    }
                }
                //资产原值
                if (StringUtils.isNotBlank(head.getKANSW())) {
                    shAssets.setOriginalWorth(Double.parseDouble(head.getKANSW()));
                }
                //净值
                if (StringUtils.isNotBlank(head.getJZJE())) {
                    shAssets.setNetValue(Double.parseDouble(head.getJZJE()));
                }
                //残值
                if (StringUtils.isNotBlank(head.getSCHRW())) {
                    shAssets.setScrapValue(Double.parseDouble(head.getSCHRW()));
                }

                //资产编码
                if(StringUtils.isNotBlank(head.getANLN1())){
                    shAssets.setSPAAssetsCode(head.getANLN1());
                }

                shAssets.setCostCode(head.getKOSTL());
                saveOrUpdate(shAssets);
            }

            generatorRspBaseInfo(dto, vo, SrmRspStatusCodeEnum.HANDLE_SUCCESS.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("资产折旧信息接口出错：" + e.getMessage());
            generatorRspBaseInfo(dto, vo, SrmRspStatusCodeEnum.INTERNAL_ERROR.getCode());
            return vo;
        }

        return vo;
    }

    /**
     * 校验报文
     *
     * @param dto 请求
     * @return 是否成功
     */
    private boolean checkData(AssetsDTO dto) {
        return true;
    }

    /**
     * 生成响应报文基础信息名称
     *
     * @param dto 请求
     * @param vo  返回
     */
    private void generatorRspBaseInfo(AssetsDTO dto, AssetsVO vo, String rspStatsCode) {
        //请求报文根节点
        AssIRequestDTO iRequest = dto.getI_REQUEST();
        //请求报文基本信息
        AssReqBaseInfoDTO reqBaseinfo = iRequest.getREQ_BASEINFO();

        //-------封装返回对象--------
        //响应报文根节点名称
        EResponseVO eResponseVO = new EResponseVO();
        vo.setE_RESPONSE(eResponseVO);

        //响应报文基础信息名称
        RspBaseInfoVO rspBaseInfoVO = new RspBaseInfoVO();
        eResponseVO.setRSP_BASEINFO(rspBaseInfoVO);
        //接收系统返回消息，产生的 TRACE_ID，消息的唯一标识
        rspBaseInfoVO.setRSP_TRACE_ID(UUID.randomUUID().toString().replace("-", ""));
        //响应请求 TRACE_ID
        rspBaseInfoVO.setRSP_REQ_TRACEID(reqBaseinfo.getREQ_TRACE_ID());
        //报文发送时间，14位数字
        rspBaseInfoVO.setRSP_SEND_TIME(Long.valueOf(DateUtils.format(new Date(), DateUtils.DATE_PATTERN_FULL)));
        //发送方系统标识
        rspBaseInfoVO.setRSP_SRC_SYS(reqBaseinfo.getREQ_SRC_SYS());
        //接收方系统标识:
        rspBaseInfoVO.setRSP_TAR_SYS("BS-WY-RFWZ");
        //PO 或 ESB 上定义的SI名称，由PO或ESB/顾问提供
        rspBaseInfoVO.setRSP_SERVER_NAME(reqBaseinfo.getREQ_SERVER_NAME());
        //业务系统流水号或业务唯一编号，从请求报文中获取
        rspBaseInfoVO.setRSP_BSN_ID(reqBaseinfo.getREQ_BSN_ID());
        //接口处理状态代码
        rspBaseInfoVO.setRSP_STATUS_CODE(rspStatsCode);

        //给srm返回的信息
        MessageVO messageVO = new MessageVO();
        RspItemVO rspItemVO = new RspItemVO();
        eResponseVO.setMESSAGE(messageVO);
        messageVO.setRSP_ITEM(rspItemVO);
        rspItemVO.setRSP_STATUS_CODE(rspStatsCode);
        rspItemVO.setRSP_STATUS_MSG(SrmRspStatusCodeEnum.getTextByCode(rspStatsCode));
    }

}
